<template>
  <div class='container'>
    <el-card class="box-card" v-loading='loading'>
      <!-- 搜索 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="关键字">
          <el-row type="flex" class="row-bg" justify="space-between">
            <el-col :span="6">
              <el-input v-model.trim="form.keyword" style="width: 200px;"></el-input>
            </el-col>
            <el-col :span="4">
              <el-button @click="clearKeyword">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <!-- 警告框 -->
       <el-alert
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom:15px;"
        >
        <template #title>
         数据一共{{total}}条
        </template>
      </el-alert>
      <!-- 表格结构 -->
       <el-table
        :data="list"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="300">
        </el-table-column>
        <el-table-column
          label="题型"
          width="100"
          prop="questionType"
          :formatter="questionType"
          >
        </el-table-column>
        <el-table-column
          prop="address"
          label="题目编号"
          >
          <template v-slot="{row}">
            <div v-for="item in row.questionIDs" :key="item.number">
              <a href="#" style="color:#09f;" @click.prevent="preview(item.id)">{{item.number}}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="addTime"
          label="录入时间"
          width="250">
        </el-table-column>
         <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
          width="150">
        </el-table-column>
         <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
          width="150">
        </el-table-column>
         <el-table-column
          prop="userName"
          label="录入人"
          width="130">
        </el-table-column>
         <el-table-column
          prop="date"
          label="操作"
          width="100">
          <template v-slot="{row}">
            <el-button
            plain
            type="danger"
            size="small"
            circle
            icon="el-icon-delete"
            title="删除"
            @click="del(row)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
       <el-row type="flex" justify="end">
         <el-pagination
          style="margin-top:15px;"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-size="pagesize"
            :page-sizes="[10, 20, 30, 40]"
            layout="prev, pager, next,sizes, jumper"
            :total="total">
          </el-pagination>
       </el-row>
    </el-card>
    <!-- 预览弹框 -->
    <QuestionsPreview4 ref="Questions" :currentId="currentId"/>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import baseApi from '@/api/base/baseApi'
import QuestionsPreview4 from '@/module-hmmm/components/questions-preview4.vue'
export default {
  name: 'questions-randoms',
  components: {
    QuestionsPreview4
  },
  data () {
    return {
      // 题目类型(数组)
      style: baseApi.style,
      // 搜索框数据
      form: {
        keyword: ''
      },
      // 加载条进度
      loading: false,
      // 表格数据
      list: [],
      // 数据列表总数
      total: 0,
      // 当前页码
      page: 1,
      // 当前页的数据条数
      pagesize: 20,
      currentId: ''

    }
  },
  created () {
    this.getQuestionsRandoms()
  },
  methods: {
    // 获取题组列表
    async getQuestionsRandoms () {
      this.loading = true
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.form.keyword

      })
      // console.log(data)
      // 赋值总条数
      this.total = data.counts
      // 赋值表格数据
      this.list = data.items
      this.loading = false
    },
    // 格式题目类型
    questionType (row, column, cellValue, index) {
      // 找到对应的题目类型返回
      // 如果拿到的为null返回非正式
      return this.style.find(item => +item.id === +cellValue)?.value
    },
    // 改变当前页的个数
    handleSizeChange (val) {
      this.pagesize = val
      this.getQuestionsRandoms()
    },
    // 改变当前页码
    handleCurrentChange (val) {
      this.page = val
      this.getQuestionsRandoms()
    },
    // 清空关键字
    clearKeyword () {
      this.form.keyword = ''
    },
    search () {
      // 页码回到第一页
      this.page = 1
      // 更新列表
      this.getQuestionsRandoms()
    },
    // 删除题组
    async  del (row) {
      try {
        await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      await removeRandoms(row)
      this.$message.success('删除题组成功')
      this.page = 1
      this.getQuestionsRandoms()
    },
    // 显示题目预览
    async preview (id) {
      this.currentId = id
      await this.$nextTick()
      this.$refs.Questions.open()
    }

  }
}
</script>

<style scoped lang='less'>
.box-card{
  margin: 15px;
}
</style>
